package com.iraeta.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.iraeta.dao.CarDao;
import com.iraeta.dao.CarJnDao;
import com.iraeta.entity.Car;
import com.iraeta.entity.CarUse;
import com.iraeta.service.CarService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CarServiceImpl implements CarService {
    @Resource
    CarDao carDao;
    @Resource
    CarJnDao carJnDao;
    @Value("${ignorecar}")
    private String ignorecar;

    /*
    timeLineLists : ["08:10-10:30", "11:07-12:52"],
    textLineLists : ["aaaaa", "bbbbb"],
    * */
    @Override
    public List<Car> listCarUse(String carNo,String carType, Date beginTime, Date endTime) {
        List<Car> carDbList = carDao.listAllCar(carNo, carType);
        List<CarUse> carUseList = carDao.listDayCarUse(DateUtil.formatDate(beginTime), DateUtil.formatDate(endTime));
        List<String> ignoreCarList = new ArrayList<>();
        if(!ignorecar.equals("none")){
            String[] ignoreCars = ignorecar.split(",");
            ignoreCarList.addAll(Arrays.asList(ignoreCars));
        }

        List<Car> carList= carDbList.stream().filter(c-> !ignoreCarList.contains(c.getCarNo())).collect(Collectors.toList());
        for (Car car : carList) {
            List<CarUse> carUseFilter = carUseList.stream().filter(x-> !StrUtil.isEmpty(x.getCarNo()) && x.getCarNo().equals(car.getCarNo())).collect(Collectors.toList());
            for (CarUse carUse : carUseFilter) {
                if(carUse.getBeginTime() == null && carUse.getEndTime() == null){
                    continue;
                }
                //判断endTime是否为空
                if(carUse.getEndTime() == null){
                    Date endTimeCalc = DateUtil.offsetHour( carUse.getBeginTime(), 1);
                    if(DateUtil.compare(endTimeCalc, endTime) > 0){
                        endTimeCalc = DateUtil.offsetMinute(endTime, -1);
                    }
                    carUse.setEndTime(endTimeCalc);
                }
                //如果endTime大于明天零点
                if(DateUtil.compare(carUse.getEndTime(), endTime) > 0){
                    carUse.setEndTime(DateUtil.offsetMinute(endTime, -1));
                }
                //如果beginTime小于今天零点
                if(carUse.getBeginTime() == null || DateUtil.compare(carUse.getBeginTime(), beginTime) < 0){
                    carUse.setBeginTime(beginTime);
                }
                String timeLine = DateUtil.format(carUse.getBeginTime(), "HH:mm") + "-" + DateUtil.format(carUse.getEndTime(), "HH:mm") ;
                String textLine = timeLine + " " + carUse.getRname()+" " + carUse.getPlace() + " " + carUse.getReason();
                car.timeLineLists.add(timeLine);
                car.textLineLists.add(textLine);
            }
            String showCarType = car.getCarType().length()>6 ? car.getCarType().substring(0,6) : car.getCarType();
            String cNo = car.getCarNo().substring(1);
            String showCarNo = showCarType + "<br/>" + cNo;
            car.setCarNo(showCarNo);
        }
//        carList.sort(new Comparator<Car>() {
//            @Override
//            public int compare(Car o1, Car o2) {
//                return o2.timeLineLists.size() - o1.timeLineLists.size();
//            }
//        });
        return carList;
    }

    @Override
    public List<Car> listCarUseForJn(String carNo, String carType, Date beginTime, Date endTime) {
        List<Car> carDbList = carJnDao.listAllCar(carNo, carType);
        List<CarUse> carUseList = carJnDao.listDayCarUse(DateUtil.formatDate(beginTime), DateUtil.formatDate(endTime));
        List<String> ignoreCarList = new ArrayList<>();
        if(!ignorecar.equals("none")){
            String[] ignoreCars = ignorecar.split(",");
            ignoreCarList.addAll(Arrays.asList(ignoreCars));
        }

        List<Car> carList= carDbList.stream().filter(c-> !ignoreCarList.contains(c.getCarNo())).collect(Collectors.toList());
        for (Car car : carList) {
            List<CarUse> carUseFilter = carUseList.stream().filter(x-> !StrUtil.isEmpty(x.getCarNo()) && x.getCarNo().equals(car.getCarNo())).collect(Collectors.toList());
            for (CarUse carUse : carUseFilter) {
                if(carUse.getBeginTime() == null && carUse.getEndTime() == null){
                    continue;
                }
                //判断endTime是否为空
                if(carUse.getEndTime() == null){
                    Date endTimeCalc = DateUtil.offsetHour( carUse.getBeginTime(), 1);
                    if(DateUtil.compare(endTimeCalc, endTime) > 0){
                        endTimeCalc = DateUtil.offsetMinute(endTime, -1);
                    }
                    carUse.setEndTime(endTimeCalc);
                }
                //如果endTime大于明天零点
                if(DateUtil.compare(carUse.getEndTime(), endTime) > 0){
                    carUse.setEndTime(DateUtil.offsetMinute(endTime, -1));
                }
                //如果beginTime小于今天零点
                if(carUse.getBeginTime() == null || DateUtil.compare(carUse.getBeginTime(), beginTime) < 0){
                    carUse.setBeginTime(beginTime);
                }
                String timeLine = DateUtil.format(carUse.getBeginTime(), "HH:mm") + "-" + DateUtil.format(carUse.getEndTime(), "HH:mm") ;
                String textLine = timeLine + " " + carUse.getRname()+" " + carUse.getPlace() + " " + carUse.getReason();
                car.timeLineLists.add(timeLine);
                car.textLineLists.add(textLine);
            }
        }
        return carList;
    }
}
